// // ******************************************************************
// //       /\ /|       @file       MediaService.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2025-01-02 20:01
// //    *(__\_\        @Copyright  Copyright (c) , 2024-12-27 18:12 Await
// // ******************************************************************

using System.ComponentModel;
using System.Net.Http.Headers;
using AutomaticSubtitles.Contracts.Dto;
using AutomaticSubtitles.Service.Application.Media.Queries;

namespace AutomaticSubtitles.Service.Services;

/// <summary>
/// 媒体服务
/// </summary>
public class MediaService : BaseService<MediaService>
{
    /// <summary>
    /// 获取媒体列表
    /// </summary>
    [EndpointSummary("获取媒体列表")]
    [Tags("媒体管理")]
    [EndpointDescription("获取分页的媒体列表")]
    [Authorize]
    public async Task<ResponseModel<PaginatedResponseDto<MediaDto>?>> GetMediaListAsync(
        [Description("页码")] int pageIndex = 1,
        [Description("每页大小")] int pageSize = 10,
        [Description("搜索关键字")] string? name = null,
        [Description("排序字段")] string? sortField = null,
        [Description("排序方式")] bool sortOrder = true,//true 升序 false 降序
        [Description("媒体类型")] int mediaType = 0)
    {
        var query = new GetMediaListQuery(pageIndex, pageSize, mediaType, name, sortField, sortOrder);
        await PublishAsync(query);
        return query.Result.CreateResult();
    }

    /// <summary>
    /// 获取媒体列表
    /// </summary>
    [EndpointSummary("获取每集信息")]
    [Tags("媒体管理")]
    [EndpointDescription("获取每集信息")]
    [Authorize]
    public async Task<ResponseModel<IReadOnlyList<MediaCollectionDto>?>> GetMediaCollectionListAsync(
        [Description("电视剧id")] string id)
    {
        var query = new GetMediaCollectionListQuery(id);
        await PublishAsync(query);
        return query.Result.CreateResult();
    }

    /// <summary>
    /// 根据ID获取媒体
    /// </summary>
    [EndpointSummary("获取媒体详情")]
    [Tags("媒体管理")]
    [EndpointDescription("根据ID获取媒体详情")]
    [Authorize]
    public async Task<ResponseModel<MediaDto?>> GetMediaByIdAsync(string id)
    {
        var query = new GetMediaByIdQuery(id);
        await PublishAsync(query);
        return query.Result.CreateResult();
    }

    /// <summary>
    ///     根据获取媒体图片数据
    /// </summary>
    [EndpointSummary("根据路径获取媒体图片数据")]
    [Tags("媒体管理")]
    [EndpointDescription("根据路径获取媒体图片数据")]
    [Authorize]
    public async Task<IResult> GetMediaImageDataBasedPath(string path)
    {
        var query = new GetMediaImageDataBasedPathQuery(path);
        await PublishAsync(query);
        // return Results.File(query.Result!);
        if (query.Result == null || query.Result.Length == 0) return Results.NotFound("未找到图片");
        if (query.Result == null || query.Result.Length == 0) return Results.NotFound("未找到图片");

        // 获取文件扩展名并转换为对应的 MIME 类型
        var extension = Path.GetExtension(path).ToLowerInvariant();
        var contentType = extension switch
        {
            ".jpg" or ".jpeg" => "image/jpeg",
            ".png" => "image/png",
            ".gif" => "image/gif",
            ".webp" => "image/webp",
            _ => "application/octet-stream"
        };

        // 设置响应头
        var httpContext = new DefaultHttpContext();
        httpContext.Response.Headers.CacheControl = $"public, max-age={3600}";
        httpContext.Response.Headers.ETag = $"\"{Path.GetFileName(path)}\"";

        return Results.Stream(
            query.Result,
            contentType,
            enableRangeProcessing: true,
            lastModified: DateTimeOffset.UtcNow
        );
    }
}